/*
 * Copyright (c) 2023 OceanBase.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

parser grammar OBOracleParser;


options { tokenVocab=OBOracleLexer; }

@parser::members {
public boolean is_pl_parse_ = false;
public boolean is_pl_parse_expr_ = false;
}


// start rule: sql_stmt

sql_stmt
    : stmt_list
    ;

stmt_list
    : EOF
    | DELIMITER
    | stmt EOF
    | stmt DELIMITER EOF?
    ;

stmt
    : select_stmt
    | insert_stmt
    | merge_stmt
    | create_table_stmt
    | alter_database_stmt
    | update_stmt
    | delete_stmt
    | drop_table_stmt
    | drop_view_stmt
    | explain_stmt
    | create_outline_stmt
    | alter_outline_stmt
    | drop_outline_stmt
    | show_stmt
    | prepare_stmt
    | variable_set_stmt
    | execute_stmt
    | call_stmt
    | alter_table_stmt
    | alter_index_stmt
    | alter_system_stmt
    | audit_stmt
    | deallocate_prepare_stmt
    | create_user_stmt
    | alter_user_stmt
    | alter_user_profile_stmt
    | drop_user_stmt
    | set_password_stmt
    | lock_user_stmt
    | grant_stmt
    | revoke_stmt
    | begin_stmt
    | commit_stmt
    | rollback_stmt
    | create_index_stmt
    | drop_index_stmt
    | kill_stmt
    | create_mlog_stmt
    | drop_mlog_stmt
    | help_stmt
    | create_view_stmt
    | create_mview_stmt
    | create_tenant_stmt
    | alter_tenant_stmt
    | drop_tenant_stmt
    | create_restore_point_stmt
    | drop_restore_point_stmt
    | create_resource_stmt
    | alter_resource_stmt
    | drop_resource_stmt
    | set_names_stmt
    | set_charset_stmt
    | create_tablegroup_stmt
    | drop_tablegroup_stmt
    | alter_tablegroup_stmt
    | rename_table_stmt
    | truncate_table_stmt
    | set_transaction_stmt
    | create_synonym_stmt
    | drop_synonym_stmt
    | create_directory_stmt
    | drop_directory_stmt
    | create_keystore_stmt
    | alter_keystore_stmt
    | create_tablespace_stmt
    | drop_tablespace_stmt
    | alter_tablespace_stmt
    | create_savepoint_stmt
    | rollback_savepoint_stmt
    | lock_tables_stmt
    | lock_table_stmt
    | unlock_tables_stmt
    | flashback_stmt
    | purge_stmt
    | create_sequence_stmt
    | alter_sequence_stmt
    | drop_sequence_stmt
    | alter_session_stmt
    | analyze_stmt
    | set_comment_stmt
    | pl_expr_stmt
    | shrink_space_stmt
    | load_data_stmt
    | create_dblink_stmt
    | drop_dblink_stmt
    | create_role_stmt
    | drop_role_stmt
    | alter_role_stmt
    | set_role_stmt
    | create_profile_stmt
    | alter_profile_stmt
    | drop_profile_stmt
    | method_opt
    | drop_package_stmt
    | drop_procedure_stmt
    | drop_function_stmt
    | drop_trigger_stmt
    | drop_type_stmt
    | create_context_stmt
    | drop_context_stmt
    | switchover_tenant_stmt
    | recover_tenant_stmt
    | transfer_partition_stmt
    | service_name_stmt
    ;

drop_package_stmt
    : DROP PACKAGE BODY? relation_factor
    ;

drop_procedure_stmt
    : DROP PROCEDURE (IF EXISTS)? relation_factor
    ;

drop_function_stmt
    : DROP FUNCTION (IF EXISTS)? relation_factor
    ;

drop_trigger_stmt
    : DROP TRIGGER relation_factor
    ;

drop_type_stmt
    : DROP TYPE BODY? relation_factor (FORCE | VALIDATE)?
    ;

pl_expr_stmt
    : DO expr
    ;

expr_list
    : bit_expr (Comma bit_expr)*
    ;

column_ref
    : column_name
    | LEVEL
    | ROWNUM
    | oracle_pl_non_reserved_words
    ;

oracle_pl_non_reserved_words
    : ACCESS
    | ADD
    | AUDIT
    | CHAR
    | COLUMN
    | COMMENT
    | CURRENT
    | DATE
    | DECIMAL
    | FILE_KEY
    | FLOAT
    | IMMEDIATE
    | INCREMENT
    | INITIAL_
    | INTEGER
    | LONG
    | MAXEXTENTS
    | MODIFY
    | NOAUDIT
    | NOTFOUND
    | NUMBER
    | OFFLINE
    | ONLINE
    | PCTFREE
    | PRIVILEGES
    | RAW
    | RENAME
    | ROW
    | ROWLABEL
    | ROWS
    | SESSION
    | SET
    | SMALLINT
    | SUCCESSFUL
    | SYNONYM
    | TRIGGER
    | VALIDATE
    | VARCHAR
    | VARCHAR2
    | WHENEVER
    | DUAL
    ;

complex_string_literal
    : STRING_VALUE
    ;

js_literal
    : literal
    ;

literal
    : complex_string_literal
    | DATE_VALUE
    | TIMESTAMP_VALUE
    | INTNUM
    | APPROXNUM
    | DECIMAL_VAL
    | NULLX
    | INTERVAL_VALUE
    ;

number_literal
    : INTNUM
    | DECIMAL_VAL
    ;

expr_const
    : literal
    | SYSTEM_VARIABLE
    | QUESTIONMARK
    | (GLOBAL_ALIAS|SESSION_ALIAS) Dot column_name
    ;

conf_const
    : STRING_VALUE
    | DATE_VALUE
    | TIMESTAMP_VALUE
    | Minus? INTNUM
    | APPROXNUM
    | Minus? DECIMAL_VAL
    | BOOL_VALUE
    | NULLX
    | SYSTEM_VARIABLE
    | (GLOBAL_ALIAS|SESSION_ALIAS) Dot column_name
    ;

bool_pri
    : bit_expr IS not? (NULLX|is_nan_inf_value)
    | bit_expr IS NOT? (JSON FORMAT)? is_json_constrain
    | bit_expr ((((COMP_EQ SOME|COMP_NSEQ)|(COMP_LE|COMP_LT) SOME)|((COMP_GE|COMP_GT? COMP_EQ)|(COMP_LE|COMP_LT COMP_EQ?)) sub_query_flag?)|(((COMP_GE|COMP_GT) SOME|(COMP_NE SOME|COMP_NE_PL))|((COMP_LT? COMP_GT|COMP_NE)|(Caret|Not) COMP_EQ) sub_query_flag?)) bit_expr
    | predicate
    ;

is_json_constrain
    : JSON strict_opt? ((scalars_opt unique_keys_opt?)?|unique_keys_opt scalars_opt?)
    | JSON LeftParen strict_opt scalars_opt? unique_keys_opt? RightParen
    | JSON LeftParen strict_opt? unique_keys_opt scalars_opt RightParen
    | JSON LeftParen scalars_opt strict_opt? unique_keys_opt? RightParen
    | JSON scalars_opt strict_opt unique_keys_opt?
    | JSON scalars_opt? unique_keys_opt strict_opt
    | JSON LeftParen scalars_opt? unique_keys_opt strict_opt RightParen
    | JSON LeftParen unique_keys_opt ((strict_opt scalars_opt)?|scalars_opt strict_opt) RightParen
    | JSON unique_keys_opt strict_opt scalars_opt
    | JSON unique_keys_opt scalars_opt strict_opt
    ;

strict_opt
    : LAX
    | STRICT
    ;

scalars_opt
    : ALLOW SCALARS
    | DISALLOW SCALARS
    ;

unique_keys_opt
    : (WITH|WITHOUT) UNIQUE KEYS
    ;

json_equal_option
    : (BOOL_VALUE|ERROR_P) ON ERROR_P
    ;

predicate
    : LNNVL LeftParen bool_pri RightParen
    | bit_expr not? IN in_expr
    | bit_expr not? BETWEEN bit_expr AND bit_expr
    | bit_expr not? LIKE bit_expr (ESCAPE bit_expr)?
    | REGEXP_LIKE LeftParen substr_params RightParen
    | exists_function_name select_with_parens
    | collection_predicate_expr
    | updating_func
    ;

collection_predicate_expr
    : bit_expr MEMBER OF? bit_expr
    | bit_expr NOT MEMBER OF? bit_expr
    | bit_expr SUBMULTISET OF? bit_expr
    | bit_expr NOT SUBMULTISET OF? bit_expr
    | bit_expr IS A_ SET
    | bit_expr IS NOT A_ SET
    | bit_expr IS EMPTY
    | bit_expr IS NOT EMPTY
    ;

bit_expr
    : bit_expr Plus bit_expr
    | bit_expr Minus bit_expr
    | bit_expr Star bit_expr
    | bit_expr Div bit_expr
    | bit_expr CNNOP bit_expr
    | bit_expr AT TIME ZONE bit_expr
    | bit_expr AT LOCAL
    | bit_expr MULTISET_OP (ALL | DISTINCT)? bit_expr
    | bit_expr POW_PL bit_expr
    | bit_expr MOD bit_expr
    | unary_expr
    | BOOL_VALUE
    ;

is_nan_inf_value
    : NAN_VALUE
    | INFINITE_VALUE
    ;

unary_expr
    : Plus? simple_expr
    | Minus simple_expr
    ;

simple_expr
    : simple_expr collation
    | obj_access_ref COLUMN_OUTER_JOIN_SYMBOL
    | expr_const
    | select_with_parens
    | CURSOR LeftParen select_stmt RightParen
    | LeftParen bit_expr RightParen
    | LeftParen expr_list Comma bit_expr RightParen
    | MATCH LeftParen column_list RightParen AGAINST LeftParen STRING_VALUE ((IN NATURAL LANGUAGE MODE) | (IN BOOLEAN MODE))? RightParen
    | case_expr
    | obj_access_ref
    | sql_function
    | cursor_attribute_expr
    | window_function
    | USER_VARIABLE
    | PLSQL_VARIABLE
    | PRIOR unary_expr
    | CONNECT_BY_ROOT unary_expr
    | SET LeftParen bit_expr RightParen
    | MULTISET select_with_parens
    | column_ref Dot column_ref USER_VARIABLE
    | column_ref Dot column_ref Dot column_ref USER_VARIABLE
    | dblink_func_expr
    | {this.is_pl_parse_}? QUESTIONMARK Dot column_name
    ;

json_function
    : json_value_expr
    | json_query_expr
    | json_mergepatch_expr
    | json_array_expr
    | json_exists_expr
    | json_object_expr
    | json_table_expr
    | json_equal_expr
    ;

gis_function
    : spatial_cellid_expr
    | spatial_mbr_expr
    | sdo_relate_expr
    ;

spatial_cellid_expr
    : SPATIAL_CELLID LeftParen bit_expr RightParen
    ;

spatial_mbr_expr
    : SPATIAL_MBR LeftParen bit_expr RightParen
    ;

sdo_relate_expr
    : SDO_RELATE LeftParen bit_expr Comma bit_expr Comma bit_expr RightParen
    ;

common_cursor_attribute
    : ISOPEN
    | FOUND
    | NOTFOUND
    | ROWCOUNT
    ;

cursor_attribute_bulk_rowcount
    : BULK_ROWCOUNT LeftParen bit_expr RightParen
    ;

cursor_attribute_bulk_exceptions
    : BULK_EXCEPTIONS Dot COUNT
    | BULK_EXCEPTIONS LeftParen bit_expr RightParen Dot (ERROR_CODE|ERROR_INDEX)
    ;

implicit_cursor_attribute
    : SQL Mod ((common_cursor_attribute|cursor_attribute_bulk_rowcount)|cursor_attribute_bulk_exceptions)
    ;

explicit_cursor_attribute
    : obj_access_ref Mod common_cursor_attribute
    ;

cursor_attribute_expr
    : explicit_cursor_attribute
    | implicit_cursor_attribute
    ;

obj_access_ref
    : column_ref ((Dot obj_access_ref) | (Dot Star))?
    | access_func_expr func_access_ref?
    | QUESTIONMARK func_access_ref
    | column_ref Dot FIRST LeftParen RightParen
    | column_ref Dot LAST LeftParen RightParen
    | column_ref Dot COUNT LeftParen RightParen
    | column_ref dot_notation_path
    | dot_notation_fun_sys
    ;

dot_notation_path
    : LeftBracket path_param_array RightBracket dot_notation_path_obj_access_ref
    ;

dot_notation_path_obj_access_ref
    : empty
    | Dot obj_access_ref
    | dot_notation_path
    ;

path_param_array
    : Star (Comma path_param_list)?
    | path_param_list
    ;

path_param_list
    : path_param (Comma path_param)*
    ;

path_param
    : INTNUM (TO path_param)?
    ;

dot_notation_fun_sys
    : dot_notation_fun
    ;

dot_notation_fun
    : func_name=(DATE|SIZE|NUMBER) LeftParen RightParen
    ;

obj_access_ref_normal
    : pl_var_name (Dot obj_access_ref_normal)?
    | access_func_expr_count ((Dot obj_access_ref_normal) | table_element_access_list)?
    | var_name LeftParen func_param_list? RightParen ((Dot obj_access_ref_normal) | table_element_access_list)?
    | PRIOR LeftParen func_param_list? RightParen ((Dot obj_access_ref_normal) | table_element_access_list)?
    | exists_function_name LeftParen func_param_list? RightParen ((Dot obj_access_ref_normal) | table_element_access_list)?
    ;

func_access_ref
    : table_element_access_list? Dot obj_access_ref
    | table_element_access_list
    ;

table_element_access_list
    : LeftParen table_index RightParen
    | table_element_access_list LeftParen table_index RightParen
    ;

table_index
    : bit_expr
    ;

expr
    : NOT expr
    | (USER_VARIABLE set_var_op)? bit_expr
    | bool_pri
    | LeftParen expr RightParen
    | expr (AND|OR) expr
    ;

not
    : NOT
    ;

sub_query_flag
    : ALL
    | ANY
    ;

in_expr
    : bit_expr
    ;

case_expr
    : CASE (bit_expr simple_when_clause_list|bool_when_clause_list) case_default END CASE?
    ;

window_function
    : func_name=COUNT LeftParen ALL? Star RightParen OVER LeftParen generalized_window_clause RightParen
    | func_name=COUNT LeftParen ALL? bit_expr RightParen OVER LeftParen generalized_window_clause RightParen
    | func_name=COUNT LeftParen DISTINCT bit_expr RightParen OVER LeftParen generalized_window_clause RightParen
    | func_name=APPROX_COUNT_DISTINCT LeftParen expr_list RightParen OVER LeftParen generalized_window_clause RightParen
    | func_name=APPROX_COUNT_DISTINCT_SYNOPSIS LeftParen expr_list RightParen OVER LeftParen generalized_window_clause RightParen
    | func_name=APPROX_COUNT_DISTINCT_SYNOPSIS_MERGE LeftParen bit_expr RightParen OVER LeftParen generalized_window_clause RightParen
    | func_name=SUM LeftParen (ALL | DISTINCT | UNIQUE)? bit_expr RightParen OVER LeftParen generalized_window_clause RightParen
    | func_name=MAX LeftParen (ALL | DISTINCT | UNIQUE)? bit_expr RightParen OVER LeftParen generalized_window_clause RightParen
    | func_name=MIN LeftParen (ALL | DISTINCT | UNIQUE)? bit_expr RightParen OVER LeftParen generalized_window_clause RightParen
    | func_name=AVG LeftParen (ALL | DISTINCT | UNIQUE)? bit_expr RightParen OVER LeftParen generalized_window_clause RightParen
    | func_name=MEDIAN LeftParen (ALL | DISTINCT | UNIQUE)? bit_expr RightParen OVER LeftParen generalized_window_clause RightParen
    | func_name=STDDEV LeftParen (ALL | DISTINCT | UNIQUE)? bit_expr RightParen OVER LeftParen generalized_window_clause RightParen
    | func_name=VARIANCE LeftParen (ALL | DISTINCT | UNIQUE)? bit_expr RightParen OVER LeftParen generalized_window_clause RightParen
    | func_name=STDDEV_POP LeftParen (ALL | DISTINCT | UNIQUE)? bit_expr RightParen OVER LeftParen generalized_window_clause RightParen
    | func_name=STDDEV_SAMP LeftParen (ALL | DISTINCT | UNIQUE)? bit_expr RightParen OVER LeftParen generalized_window_clause RightParen
    | func_name=LISTAGG LeftParen (ALL | DISTINCT | UNIQUE)? expr_list RightParen WITHIN GROUP LeftParen order_by RightParen OVER LeftParen generalized_window_clause RightParen
    | func_name=LISTAGG LeftParen (ALL | DISTINCT | UNIQUE)? expr_list RightParen OVER LeftParen generalized_window_clause RightParen
    | func_name=RANK LeftParen RightParen OVER LeftParen generalized_window_clause RightParen
    | func_name=DENSE_RANK LeftParen RightParen OVER LeftParen generalized_window_clause RightParen
    | func_name=PERCENT_RANK LeftParen RightParen OVER LeftParen generalized_window_clause RightParen
    | func_name=ROW_NUMBER LeftParen RightParen OVER LeftParen generalized_window_clause RightParen
    | func_name=NTILE LeftParen bit_expr RightParen OVER LeftParen generalized_window_clause RightParen
    | func_name=CUME_DIST LeftParen RightParen OVER LeftParen generalized_window_clause RightParen
    | func_name=FIRST_VALUE win_fun_first_last_params OVER LeftParen generalized_window_clause RightParen
    | func_name=LAST_VALUE win_fun_first_last_params OVER LeftParen generalized_window_clause RightParen
    | func_name=LEAD win_fun_lead_lag_params OVER LeftParen generalized_window_clause RightParen
    | func_name=LAG win_fun_lead_lag_params OVER LeftParen generalized_window_clause RightParen
    | func_name=NTH_VALUE LeftParen bit_expr Comma bit_expr RightParen (FROM first_or_last)? (respect_or_ignore NULLS)? OVER LeftParen generalized_window_clause RightParen
    | func_name=RATIO_TO_REPORT LeftParen (ALL | DISTINCT | UNIQUE)? bit_expr RightParen OVER LeftParen generalized_window_clause RightParen
    | func_name=CORR LeftParen (ALL | DISTINCT | UNIQUE)? bit_expr Comma bit_expr RightParen OVER LeftParen generalized_window_clause RightParen
    | func_name=COVAR_POP LeftParen (ALL | DISTINCT | UNIQUE)? bit_expr Comma bit_expr RightParen OVER LeftParen generalized_window_clause RightParen
    | func_name=COVAR_SAMP LeftParen (ALL | DISTINCT | UNIQUE)? bit_expr Comma bit_expr RightParen OVER LeftParen generalized_window_clause RightParen
    | func_name=VAR_POP LeftParen (ALL | DISTINCT | UNIQUE)? bit_expr RightParen OVER LeftParen generalized_window_clause RightParen
    | func_name=VAR_SAMP LeftParen (ALL | DISTINCT | UNIQUE)? bit_expr RightParen OVER LeftParen generalized_window_clause RightParen
    | func_name=REGR_SLOPE LeftParen (ALL | DISTINCT | UNIQUE)? bit_expr Comma bit_expr RightParen OVER LeftParen generalized_window_clause RightParen
    | func_name=REGR_INTERCEPT LeftParen (ALL | DISTINCT | UNIQUE)? bit_expr Comma bit_expr RightParen OVER LeftParen generalized_window_clause RightParen
    | func_name=REGR_COUNT LeftParen (ALL | DISTINCT | UNIQUE)? bit_expr Comma bit_expr RightParen OVER LeftParen generalized_window_clause RightParen
    | func_name=REGR_R2 LeftParen (ALL | DISTINCT | UNIQUE)? bit_expr Comma bit_expr RightParen OVER LeftParen generalized_window_clause RightParen
    | func_name=REGR_AVGX LeftParen (ALL | DISTINCT | UNIQUE)? bit_expr Comma bit_expr RightParen OVER LeftParen generalized_window_clause RightParen
    | func_name=REGR_AVGY LeftParen (ALL | DISTINCT | UNIQUE)? bit_expr Comma bit_expr RightParen OVER LeftParen generalized_window_clause RightParen
    | func_name=REGR_SXX LeftParen (ALL | DISTINCT | UNIQUE)? bit_expr Comma bit_expr RightParen OVER LeftParen generalized_window_clause RightParen
    | func_name=REGR_SYY LeftParen (ALL | DISTINCT | UNIQUE)? bit_expr Comma bit_expr RightParen OVER LeftParen generalized_window_clause RightParen
    | func_name=REGR_SXY LeftParen (ALL | DISTINCT | UNIQUE)? bit_expr Comma bit_expr RightParen OVER LeftParen generalized_window_clause RightParen
    | func_name=MAX LeftParen (ALL | DISTINCT | UNIQUE)? bit_expr RightParen KEEP LeftParen DENSE_RANK first_or_last order_by RightParen OVER LeftParen generalized_window_clause RightParen
    | func_name=MIN LeftParen (ALL | DISTINCT | UNIQUE)? bit_expr RightParen KEEP LeftParen DENSE_RANK first_or_last order_by RightParen OVER LeftParen generalized_window_clause RightParen
    | func_name=SUM LeftParen (ALL | DISTINCT | UNIQUE)? bit_expr RightParen KEEP LeftParen DENSE_RANK first_or_last order_by RightParen OVER LeftParen generalized_window_clause RightParen
    | func_name=COUNT LeftParen ALL? Star RightParen KEEP LeftParen DENSE_RANK first_or_last order_by RightParen OVER LeftParen generalized_window_clause RightParen
    | func_name=COUNT LeftParen ALL? bit_expr RightParen KEEP LeftParen DENSE_RANK first_or_last order_by RightParen OVER LeftParen generalized_window_clause RightParen
    | func_name=AVG LeftParen (ALL | DISTINCT | UNIQUE)? bit_expr RightParen KEEP LeftParen DENSE_RANK first_or_last order_by RightParen OVER LeftParen generalized_window_clause RightParen
    | func_name=VARIANCE LeftParen (ALL | DISTINCT | UNIQUE)? bit_expr RightParen KEEP LeftParen DENSE_RANK first_or_last order_by RightParen OVER LeftParen generalized_window_clause RightParen
    | func_name=STDDEV LeftParen (ALL | DISTINCT | UNIQUE)? bit_expr RightParen KEEP LeftParen DENSE_RANK first_or_last order_by RightParen OVER LeftParen generalized_window_clause RightParen
    | func_name=PERCENTILE_CONT LeftParen (ALL | DISTINCT | UNIQUE)? expr_list RightParen WITHIN GROUP LeftParen order_by RightParen OVER LeftParen generalized_window_clause RightParen
    | func_name=PERCENTILE_DISC LeftParen (ALL | DISTINCT | UNIQUE)? expr_list RightParen WITHIN GROUP LeftParen order_by RightParen OVER LeftParen generalized_window_clause RightParen
    | func_name=WM_CONCAT LeftParen (ALL | DISTINCT | UNIQUE)? bit_expr RightParen OVER LeftParen generalized_window_clause RightParen
    | func_name=WMSYS Dot sub_func_name=WM_CONCAT LeftParen (ALL | DISTINCT | UNIQUE)? bit_expr RightParen OVER LeftParen generalized_window_clause RightParen
    | func_name=WM_CONCAT LeftParen (ALL | DISTINCT | UNIQUE)? bit_expr RightParen KEEP LeftParen DENSE_RANK first_or_last order_by RightParen OVER LeftParen generalized_window_clause RightParen
    | func_name=WMSYS Dot sub_func_name=WM_CONCAT LeftParen (ALL | DISTINCT | UNIQUE)? bit_expr RightParen KEEP LeftParen DENSE_RANK first_or_last order_by RightParen OVER LeftParen generalized_window_clause RightParen
    | func_name=TOP_K_FRE_HIST LeftParen bit_expr Comma bit_expr Comma bit_expr RightParen OVER LeftParen generalized_window_clause RightParen
    | func_name=HYBRID_HIST LeftParen bit_expr Comma bit_expr RightParen OVER LeftParen generalized_window_clause RightParen
    | function_name LeftParen func_param_list RightParen OVER LeftParen generalized_window_clause RightParen
    | function_name LeftParen ALL func_param_list RightParen OVER LeftParen generalized_window_clause RightParen
    | function_name LeftParen DISTINCT func_param_list RightParen OVER LeftParen generalized_window_clause RightParen
    | function_name LeftParen UNIQUE func_param_list RightParen OVER LeftParen generalized_window_clause RightParen
    ;

first_or_last
    : FIRST
    | LAST
    ;

respect_or_ignore
    : RESPECT
    | IGNORE
    ;

win_fun_first_last_params
    : LeftParen bit_expr respect_or_ignore NULLS RightParen
    | LeftParen bit_expr RightParen (respect_or_ignore NULLS)?
    ;

win_fun_lead_lag_params
    : LeftParen bit_expr respect_or_ignore NULLS RightParen
    | LeftParen bit_expr respect_or_ignore NULLS Comma expr_list RightParen
    | LeftParen expr_list RightParen (respect_or_ignore NULLS)?
    ;

generalized_window_clause
    : (PARTITION BY expr_list)? order_by? win_window?
    ;

win_rows_or_range
    : ROWS
    | RANGE
    ;

win_preceding_or_following
    : PRECEDING
    | FOLLOWING
    ;

win_interval
    : bit_expr
    ;

win_bounding
    : CURRENT ROW
    | win_interval win_preceding_or_following
    ;

win_window
    : win_rows_or_range BETWEEN win_bounding AND win_bounding
    | win_rows_or_range win_bounding
    ;

simple_when_clause_list
    : simple_when_clause+
    ;

simple_when_clause
    : WHEN bit_expr THEN bit_expr
    ;

bool_when_clause_list
    : bool_when_clause+
    ;

bool_when_clause
    : WHEN expr THEN bit_expr
    ;

case_default
    : ELSE bit_expr
    | empty
    ;

sql_function
    : single_row_function
    | aggregate_function
    | special_func_expr
    ;

xml_function
    : xmlparse_expr
    | xml_element_expr
    | xml_extract_expr
    | delete_xml
    | insert_child_xml
    | xmlserialize_expr
    | xmlcast_expr
    | xml_sequence_expr
    ;

single_row_function
    : numeric_function
    | character_function
    | extract_function
    | conversion_function
    | hierarchical_function
    | environment_id_function
    | json_function
    | xml_function ((Dot obj_access_ref_normal) | table_element_access_list)?
    | gis_function
    ;

numeric_function
    : MOD LeftParen bit_expr Comma bit_expr RightParen
    ;

character_function
    : TRIM LeftParen parameterized_trim RightParen
    | ASCII LeftParen bit_expr RightParen
    | TRANSLATE LeftParen bit_expr USING translate_charset RightParen
    | TRANSLATE LeftParen bit_expr Comma bit_expr Comma bit_expr RightParen
    ;

translate_charset
    : CHAR_CS
    | NCHAR_CS
    ;

extract_function
    : EXTRACT LeftParen date_unit_for_extract FROM bit_expr RightParen
    ;

conversion_function
    : CAST LeftParen bit_expr AS cast_data_type RightParen
    | TREAT LeftParen bit_expr AS treat_data_type RightParen
    ;

hierarchical_function
    : SYS_CONNECT_BY_PATH LeftParen bit_expr Comma bit_expr RightParen
    ;

environment_id_function
    : USER
    | UID
    ;

aggregate_function
    : funcName=APPROX_COUNT_DISTINCT LeftParen DISTINCT? expr_list RightParen
    | funcName=APPROX_COUNT_DISTINCT_SYNOPSIS LeftParen expr_list RightParen
    | funcName=APPROX_COUNT_DISTINCT_SYNOPSIS_MERGE LeftParen bit_expr RightParen
    | funcName=SUM LeftParen (ALL | DISTINCT | UNIQUE)? bit_expr RightParen
    | funcName=MAX LeftParen (ALL | DISTINCT | UNIQUE)? bit_expr RightParen
    | funcName=MIN LeftParen (ALL | DISTINCT | UNIQUE)? bit_expr RightParen
    | funcName=AVG LeftParen (ALL | DISTINCT | UNIQUE)? bit_expr RightParen
    | funcName=JSON_ARRAYAGG LeftParen (ALL | DISTINCT | UNIQUE)? bit_expr (FORMAT JSON)? order_by? js_agg_on_null? js_agg_returning_type_opt? STRICT? RightParen
    | funcName=JSON_OBJECTAGG LeftParen KEY? bit_expr VALUE bit_expr (FORMAT JSON)? js_agg_on_null? js_agg_returning_type_opt? STRICT? json_obj_unique_key? RightParen
    | funcName=JSON_OBJECTAGG LeftParen bit_expr Comma bit_expr (FORMAT JSON)? js_agg_on_null? js_agg_returning_type_opt? STRICT? json_obj_unique_key? RightParen
    | funcName=MEDIAN LeftParen (ALL | DISTINCT | UNIQUE)? bit_expr RightParen
    | funcName=STDDEV LeftParen (ALL | DISTINCT | UNIQUE)? bit_expr RightParen
    | funcName=VARIANCE LeftParen (ALL | DISTINCT | UNIQUE)? bit_expr RightParen
    | funcName=STDDEV_POP LeftParen (ALL | DISTINCT | UNIQUE)? bit_expr RightParen
    | funcName=STDDEV_SAMP LeftParen (ALL | DISTINCT | UNIQUE)? bit_expr RightParen
    | funcName=GROUPING LeftParen bit_expr RightParen
    | funcName=LISTAGG LeftParen (ALL | DISTINCT | UNIQUE)? expr_list RightParen WITHIN GROUP LeftParen order_by RightParen
    | funcName=LISTAGG LeftParen (ALL | DISTINCT | UNIQUE)? expr_list RightParen
    | funcName=CORR LeftParen (ALL | DISTINCT | UNIQUE)? bit_expr Comma bit_expr RightParen
    | funcName=COVAR_POP LeftParen (ALL | DISTINCT | UNIQUE)? bit_expr Comma bit_expr RightParen
    | funcName=COVAR_SAMP LeftParen (ALL | DISTINCT | UNIQUE)? bit_expr Comma bit_expr RightParen
    | funcName=VAR_POP LeftParen (ALL | DISTINCT | UNIQUE)? bit_expr RightParen
    | funcName=VAR_SAMP LeftParen (ALL | DISTINCT | UNIQUE)? bit_expr RightParen
    | funcName=REGR_SLOPE LeftParen (ALL | DISTINCT | UNIQUE)? bit_expr Comma bit_expr RightParen
    | funcName=REGR_INTERCEPT LeftParen (ALL | DISTINCT | UNIQUE)? bit_expr Comma bit_expr RightParen
    | funcName=REGR_COUNT LeftParen (ALL | DISTINCT | UNIQUE)? bit_expr Comma bit_expr RightParen
    | funcName=REGR_R2 LeftParen (ALL | DISTINCT | UNIQUE)? bit_expr Comma bit_expr RightParen
    | funcName=REGR_AVGX LeftParen (ALL | DISTINCT | UNIQUE)? bit_expr Comma bit_expr RightParen
    | funcName=REGR_AVGY LeftParen (ALL | DISTINCT | UNIQUE)? bit_expr Comma bit_expr RightParen
    | funcName=REGR_SXX LeftParen (ALL | DISTINCT | UNIQUE)? bit_expr Comma bit_expr RightParen
    | funcName=REGR_SYY LeftParen (ALL | DISTINCT | UNIQUE)? bit_expr Comma bit_expr RightParen
    | funcName=REGR_SXY LeftParen (ALL | DISTINCT | UNIQUE)? bit_expr Comma bit_expr RightParen
    | funcName=RANK LeftParen (ALL | DISTINCT | UNIQUE)? expr_list RightParen WITHIN GROUP LeftParen order_by RightParen
    | funcName=PERCENT_RANK LeftParen (ALL | DISTINCT | UNIQUE)? expr_list RightParen WITHIN GROUP LeftParen order_by RightParen
    | funcName=DENSE_RANK LeftParen (ALL | DISTINCT | UNIQUE)? expr_list RightParen WITHIN GROUP LeftParen order_by RightParen
    | funcName=CUME_DIST LeftParen (ALL | DISTINCT | UNIQUE)? expr_list RightParen WITHIN GROUP LeftParen order_by RightParen
    | funcName=PERCENTILE_CONT LeftParen (ALL | DISTINCT | UNIQUE)? expr_list RightParen WITHIN GROUP LeftParen order_by RightParen
    | funcName=PERCENTILE_DISC LeftParen (ALL | DISTINCT | UNIQUE)? expr_list RightParen WITHIN GROUP LeftParen order_by RightParen
    | funcName=MAX LeftParen (ALL | DISTINCT | UNIQUE)? bit_expr RightParen KEEP LeftParen DENSE_RANK first_or_last order_by RightParen
    | funcName=MIN LeftParen (ALL | DISTINCT | UNIQUE)? bit_expr RightParen KEEP LeftParen DENSE_RANK first_or_last order_by RightParen
    | funcName=SUM LeftParen (ALL | DISTINCT | UNIQUE)? bit_expr RightParen KEEP LeftParen DENSE_RANK first_or_last order_by RightParen
    | funcName=AVG LeftParen (ALL | DISTINCT | UNIQUE)? bit_expr RightParen KEEP LeftParen DENSE_RANK first_or_last order_by RightParen
    | funcName=VARIANCE LeftParen (ALL | DISTINCT | UNIQUE)? bit_expr RightParen KEEP LeftParen DENSE_RANK first_or_last order_by RightParen
    | funcName=STDDEV LeftParen (ALL | DISTINCT | UNIQUE)? bit_expr RightParen KEEP LeftParen DENSE_RANK first_or_last order_by RightParen
    | funcName=WM_CONCAT LeftParen (ALL | DISTINCT | UNIQUE)? bit_expr RightParen
    | funcName=WMSYS Dot subFuncName=WM_CONCAT LeftParen (ALL | DISTINCT | UNIQUE)? bit_expr RightParen
    | funcName=WM_CONCAT LeftParen (ALL | DISTINCT | UNIQUE)? bit_expr RightParen KEEP LeftParen DENSE_RANK first_or_last order_by RightParen
    | funcName=WMSYS Dot subFuncName=WM_CONCAT LeftParen (ALL | DISTINCT | UNIQUE)? bit_expr RightParen KEEP LeftParen DENSE_RANK first_or_last order_by RightParen
    | funcName=TOP_K_FRE_HIST LeftParen bit_expr Comma bit_expr Comma bit_expr RightParen
    | funcName=HYBRID_HIST LeftParen bit_expr Comma bit_expr RightParen
    | funcName=XMLAGG LeftParen simple_expr order_by? RightParen
    | funcName=SUM_OPNSIZE LeftParen bit_expr RightParen
    ;

js_agg_on_null
    : (ABSENT | NULLX) ON NULLX
    ;

js_agg_returning_type_opt
    : RETURNING js_return_type
    | RETURNING js_agg_returning_type
    ;

js_agg_returning_type
    : RAW LeftParen zero_suffix_intnum RightParen
    | RAW
    | NVARCHAR2 nstring_length_i?
    ;

special_func_expr
    : (((DATE|ISNULL)|(TIME|YEAR))|MONTH) LeftParen bit_expr RightParen
    | cur_timestamp_func
    | INSERT LeftParen bit_expr Comma bit_expr Comma bit_expr Comma bit_expr RightParen
    | (CALC_PARTITION_ID|LEFT) LeftParen bit_expr Comma bit_expr RightParen
    | POSITION LeftParen bit_expr IN bit_expr RightParen
    | (DEFAULT|VALUES) LeftParen column_definition_ref RightParen
    | CALC_PARTITION_ID LeftParen bit_expr Comma bit_expr Comma bit_expr RightParen
    | LAST_REFRESH_SCN LeftParen INTNUM RightParen
    ;

access_func_expr_count
    : COUNT LeftParen ALL? Star RightParen
    | COUNT LeftParen ALL? bit_expr RightParen
    | COUNT LeftParen DISTINCT bit_expr RightParen
    | COUNT LeftParen UNIQUE bit_expr RightParen
    | COUNT LeftParen ALL? Star RightParen KEEP LeftParen DENSE_RANK first_or_last order_by RightParen
    | COUNT LeftParen ALL? bit_expr RightParen KEEP LeftParen DENSE_RANK first_or_last order_by RightParen
    ;

access_func_expr
    : access_func_expr_count
    | function_name LeftParen RightParen
    | function_name LeftParen func_param_list RightParen
    | NEW NAME_OB LeftParen RightParen
    | NEW NAME_OB LeftParen func_param_list RightParen
    | function_name LeftParen func_param_list opt_json_exist RightParen
    | function_name LeftParen func_param_list json_equal_option RightParen
    | aggregate_function_keyword LeftParen RightParen
    | aggregate_function_keyword LeftParen func_param_list RightParen
    | function_name LeftParen ALL func_param_list RightParen
    | function_name LeftParen DISTINCT func_param_list RightParen
    | function_name LeftParen UNIQUE func_param_list RightParen
    | exists_function_name LeftParen func_param_list? RightParen
    ;

dblink_func_expr
    : column_ref USER_VARIABLE LeftParen func_param_list? RightParen
    | column_ref Dot column_ref USER_VARIABLE LeftParen func_param_list? RightParen
    | column_ref Dot column_ref Dot column_ref USER_VARIABLE LeftParen func_param_list? RightParen
    ;

func_param_list
    : func_param (Comma func_param)*
    ;

func_param
    : func_param_with_assign
    | bit_expr
    | bool_pri_in_pl_func
    ;

func_param_with_assign
    : pl_var_name PARAM_ASSIGN_OPERATOR (bit_expr|bool_pri_in_pl_func)
    ;

pl_var_name
    : var_name
    | oracle_pl_non_reserved_words
    ;

bool_pri_in_pl_func
    : bool_pri
    | NOT bool_pri_in_pl_func
    | LeftParen bool_pri_in_pl_func RightParen
    | bool_pri_in_pl_func (AND|OR) bool_pri_in_pl_func
    ;

cur_timestamp_func
    : SYSDATE
    | ((LOCALTIMESTAMP|SYSTIMESTAMP)|CURRENT_TIMESTAMP) LeftParen INTNUM RightParen
    ;

updating_func
    : UPDATING LeftParen updating_params RightParen
    ;

updating_params
    : STRING_VALUE
    | pl_var_name
    ;

substr_params
    : bit_expr Comma bit_expr (Comma bit_expr)?
    ;

returning_log_error_clause
    : returning_clause
    | log_error_clause
    | returning_clause log_error_clause
    ;

returning_clause
    : (RETURNING | RETURN) returning_exprs opt_into_clause
    ;

log_error_clause
    : LOG ERRORS into_err_log_caluse opt_simple_expression reject_limit
    ;

delete_stmt
    : delete_with_opt_hint FROM table_factor opt_where_extension returning_log_error_clause?
    | delete_with_opt_hint table_factor ((WHERE expr) | (WHERE HINT_VALUE expr))? returning_log_error_clause?
    ;

update_stmt
    : update_with_opt_hint dml_table_clause SET update_asgn_list opt_where_extension returning_log_error_clause?
    ;

update_asgn_list
    : normal_asgn_list
    | ROW COMP_EQ obj_access_ref_normal
    ;

normal_asgn_list
    : update_asgn_factor (Comma update_asgn_factor)*
    ;

update_asgn_factor
    : column_definition_ref COMP_EQ expr_or_default
    | LeftParen column_list RightParen COMP_EQ LeftParen subquery order_by? fetch_next_clause? RightParen
    ;

create_resource_stmt
    : CREATE RESOURCE UNIT relation_name (resource_unit_option | (opt_resource_unit_option_list Comma resource_unit_option))?
    | CREATE RESOURCE POOL relation_name (create_resource_pool_option | (opt_create_resource_pool_option_list Comma create_resource_pool_option))?
    ;

opt_resource_unit_option_list
    : resource_unit_option
    | empty
    | opt_resource_unit_option_list Comma resource_unit_option
    ;

resource_unit_option
    : MIN_CPU COMP_EQ? conf_const
    | MIN_IOPS COMP_EQ? conf_const
    | MIN_MEMORY COMP_EQ? conf_const
    | MAX_CPU COMP_EQ? conf_const
    | MAX_MEMORY COMP_EQ? conf_const
    | MEMORY_SIZE COMP_EQ? conf_const
    | MAX_IOPS COMP_EQ? conf_const
    | MAX_DISK_SIZE COMP_EQ? conf_const
    | MAX_SESSION_NUM COMP_EQ? conf_const
    | IOPS_WEIGHT COMP_EQ? conf_const
    | LOG_DISK_SIZE COMP_EQ? conf_const
    ;

opt_create_resource_pool_option_list
    : create_resource_pool_option
    | empty
    | opt_create_resource_pool_option_list Comma create_resource_pool_option
    ;

create_resource_pool_option
    : UNIT COMP_EQ? relation_name_or_string
    | UNIT_NUM COMP_EQ? INTNUM
    | ZONE_LIST COMP_EQ? LeftParen zone_list RightParen
    | REPLICA_TYPE COMP_EQ? STRING_VALUE
    ;

alter_resource_pool_option_list
    : alter_resource_pool_option (Comma alter_resource_pool_option)*
    ;

id_list
    : INTNUM (Comma INTNUM)*
    ;

alter_resource_pool_option
    : UNIT COMP_EQ? relation_name_or_string
    | UNIT_NUM COMP_EQ? INTNUM (DELETE UNIT opt_equal_mark LeftParen id_list RightParen)?
    | ZONE_LIST COMP_EQ? LeftParen zone_list RightParen
    ;

alter_resource_stmt
    : ALTER RESOURCE UNIT relation_name (resource_unit_option | (opt_resource_unit_option_list Comma resource_unit_option))?
    | ALTER RESOURCE POOL relation_name alter_resource_pool_option_list
    | ALTER RESOURCE POOL relation_name SPLIT INTO LeftParen resource_pool_list RightParen ON LeftParen zone_list RightParen
    | ALTER RESOURCE POOL MERGE LeftParen resource_pool_list RightParen INTO LeftParen resource_pool_list RightParen
    | ALTER RESOURCE TENANT relation_name UNIT_NUM COMP_EQ? INTNUM (DELETE UNIT_GROUP opt_equal_mark LeftParen id_list RightParen)?
    ;

drop_resource_stmt
    : DROP RESOURCE (POOL|UNIT) relation_name
    ;

create_tenant_stmt
    : CREATE TENANT relation_name (tenant_option | (opt_tenant_option_list Comma tenant_option))? ((SET sys_var_and_val_list) | (SET VARIABLES sys_var_and_val_list) | (VARIABLES sys_var_and_val_list))?
    ;

opt_tenant_option_list
    : tenant_option
    | empty
    | opt_tenant_option_list Comma tenant_option
    ;

tenant_option
    : LOGONLY_REPLICA_NUM COMP_EQ? INTNUM
    | LOCALITY COMP_EQ? STRING_VALUE FORCE?
    | REPLICA_NUM COMP_EQ? INTNUM
    | REWRITE_MERGE_VERSION COMP_EQ? INTNUM
    | STORAGE_FORMAT_VERSION COMP_EQ? INTNUM
    | STORAGE_FORMAT_WORK_VERSION COMP_EQ? INTNUM
    | PRIMARY_ZONE COMP_EQ? primary_zone_name
    | RESOURCE_POOL_LIST COMP_EQ? LeftParen resource_pool_list RightParen
    | ZONE_LIST COMP_EQ? LeftParen zone_list RightParen
    | charset_key COMP_EQ? charset_name
    | read_only_or_write
    | COMMENT COMP_EQ? STRING_VALUE
    | default_tablegroup
    | ENABLE_EXTENDED_ROWID COMP_EQ? BOOL_VALUE
    ;

zone_list
    : STRING_VALUE (opt_comma STRING_VALUE)*
    ;

resource_pool_list
    : STRING_VALUE (Comma STRING_VALUE)*
    ;

alter_tenant_stmt
    : ALTER TENANT relation_name SET? (tenant_option | (opt_tenant_option_list Comma tenant_option))? (VARIABLES sys_var_and_val_list)?
    | ALTER TENANT relation_name lock_spec_mysql57
    ;

drop_tenant_stmt
    : DROP TENANT relation_name
    ;

create_restore_point_stmt
    : CREATE RESTORE POINT relation_name
    ;

drop_restore_point_stmt
    : DROP RESTORE POINT relation_name
    ;

database_key
    : DATABASE
    | SCHEMA
    ;

database_factor
    : relation_name
    ;

database_option_list
    : database_option+
    ;

charset_key
    : CHARSET
    | CHARACTER SET
    ;

database_option
    : DEFAULT? charset_key COMP_EQ? charset_name
    | REPLICA_NUM COMP_EQ? INTNUM
    | PRIMARY_ZONE COMP_EQ? primary_zone_name
    | read_only_or_write
    | default_tablegroup
    | DATABASE_ID COMP_EQ? INTNUM
    ;

read_only_or_write
    : READ ONLY
    | READ WRITE
    ;

alter_database_stmt
    : ALTER database_key database_name? SET? database_option_list
    ;

database_name
    : NAME_OB
    ;

load_data_stmt
    : load_data_with_opt_hint (LOCAL | REMOTE_OSS)? INFILE STRING_VALUE (IGNORE | REPLACE)? INTO TABLE relation_factor (COMPRESSION opt_equal_mark compression_name)? (CHARACTER SET charset_name_or_default)? field_opt line_opt (IGNORE INTNUM lines_or_rows)? ((LeftParen RightParen) | (LeftParen field_or_vars_list RightParen))? (SET load_set_list)? load_data_extended_option_list?
    | load_data_with_opt_hint (LOCAL | REMOTE_OSS)? INFILE STRING_VALUE (IGNORE | REPLACE)? INTO TABLE relation_factor use_partition (COMPRESSION opt_equal_mark compression_name)? (CHARACTER SET charset_name_or_default)? field_opt line_opt (IGNORE INTNUM lines_or_rows)? ((LeftParen RightParen) | (LeftParen field_or_vars_list RightParen))? (SET load_set_list)? load_data_extended_option_list?
    ;

load_data_with_opt_hint
    : LOAD DATA
    | LOAD_DATA_HINT_BEGIN hint_list_with_end
    ;

compression_name
    : NAME_OB
    | unreserved_keyword
    ;

lines_or_rows
    : LINES
    | ROWS
    ;

field_or_vars_list
    : field_or_vars (Comma field_or_vars)*
    ;

field_or_vars
    : column_definition_ref
    | USER_VARIABLE
    ;

load_set_list
    : load_set_element (Comma load_set_element)*
    ;

load_set_element
    : column_definition_ref COMP_EQ expr_or_default
    ;

load_data_extended_option_list
    : load_data_extended_option load_data_extended_option_list?
    ;

load_data_extended_option
    : LOGFILE COMP_EQ? STRING_VALUE
    | REJECT LIMIT COMP_EQ? INTNUM
    | BADFILE COMP_EQ? STRING_VALUE
    ;

create_synonym_stmt
    : CREATE (OR REPLACE)? PUBLIC? SYNONYM synonym_name FOR synonym_object (USER_VARIABLE opt_reverse_link_flag)?
    | CREATE (OR REPLACE)? PUBLIC? SYNONYM database_factor Dot synonym_name FOR synonym_object (USER_VARIABLE opt_reverse_link_flag)?
    | CREATE (OR REPLACE)? PUBLIC? SYNONYM synonym_name FOR database_factor Dot synonym_object (USER_VARIABLE opt_reverse_link_flag)?
    | CREATE (OR REPLACE)? PUBLIC? SYNONYM database_factor Dot synonym_name FOR database_factor Dot synonym_object (USER_VARIABLE opt_reverse_link_flag)?
    ;

synonym_name
    : NAME_OB
    | unreserved_keyword
    ;

synonym_object
    : NAME_OB
    | unreserved_keyword
    ;

drop_synonym_stmt
    : DROP PUBLIC? SYNONYM synonym_name FORCE?
    | DROP PUBLIC? SYNONYM database_factor Dot synonym_name FORCE?
    ;

special_table_type
    : GLOBAL TEMPORARY
    | EXTERNAL
    | empty
    ;

on_commit_option
    : ON COMMIT (DELETE|PRESERVE) ROWS
    | empty
    ;

create_directory_stmt
    : CREATE (OR REPLACE)? DIRECTORY directory_name AS directory_path
    ;

directory_name
    : NAME_OB
    | unreserved_keyword
    ;

directory_path
    : STRING_VALUE
    ;

drop_directory_stmt
    : DROP DIRECTORY directory_name
    ;

create_keystore_stmt
    : ADMINISTER KEY MANAGEMENT CREATE KEYSTORE keystore_name IDENTIFIED BY password
    ;

alter_keystore_stmt
    : ADMINISTER KEY MANAGEMENT ALTER KEYSTORE PASSWORD IDENTIFIED BY password SET password
    | ADMINISTER KEY MANAGEMENT SET ((KEY|KEYSTORE CLOSE)|KEYSTORE OPEN) IDENTIFIED BY password
    ;

create_table_stmt
    : CREATE special_table_type TABLE relation_factor LeftParen table_element_list RightParen table_option_list? (partition_option | auto_partition_option)? with_column_group? on_commit_option
    | CREATE special_table_type TABLE relation_factor LeftParen table_element_list RightParen table_option_list? (partition_option | auto_partition_option)? with_column_group? on_commit_option AS subquery order_by? fetch_next_clause?
    | CREATE special_table_type TABLE relation_factor table_option_list (partition_option | auto_partition_option)? with_column_group? on_commit_option AS subquery order_by? fetch_next_clause?
    | CREATE special_table_type TABLE relation_factor partition_option with_column_group? on_commit_option AS subquery order_by? fetch_next_clause?
    | CREATE special_table_type TABLE relation_factor with_column_group? on_commit_option AS subquery order_by? fetch_next_clause?
    ;

table_element_list
    : table_element (Comma table_element)*
    ;

table_element
    : column_definition
    | out_of_line_constraint
    | out_of_line_index
    ;

column_definition
    : column_definition_ref data_type visibility_option? opt_column_attribute_list?
    | column_definition_ref data_type visibility_option? (GENERATED opt_generated_option_list)? AS LeftParen bit_expr RightParen VIRTUAL? opt_generated_column_attribute_list?
    | column_definition_ref visibility_option? (GENERATED opt_generated_option_list)? AS LeftParen bit_expr RightParen VIRTUAL? opt_generated_column_attribute_list?
    | column_definition_ref data_type visibility_option? (GENERATED opt_generated_option_list)? AS opt_identity_attribute sequence_option_list? opt_column_attribute_list?
    | column_definition_ref visibility_option? opt_column_attribute_list?
    | column_definition_ref visibility_option? (GENERATED opt_generated_option_list)? AS opt_identity_attribute sequence_option_list? opt_column_attribute_list?
    ;

column_definition_opt_datatype
    : column_definition_ref opt_column_attribute_list?
    | column_definition_ref visibility_option opt_column_attribute_list?
    | column_definition_ref data_type visibility_option? opt_column_attribute_list?
    | column_definition_ref visibility_option? (GENERATED opt_generated_option_list)? AS LeftParen bit_expr RightParen VIRTUAL? opt_generated_column_attribute_list?
    | column_definition_ref data_type visibility_option? (GENERATED opt_generated_option_list)? AS LeftParen bit_expr RightParen VIRTUAL? opt_generated_column_attribute_list?
    | column_definition_ref data_type visibility_option? (GENERATED opt_generated_option_list)? AS opt_identity_attribute sequence_option_list? opt_column_attribute_list?
    ;

out_of_line_index
    : INDEX index_using_algorithm? LeftParen sort_column_list RightParen opt_index_options?
    | INDEX index_name index_using_algorithm? LeftParen sort_column_list RightParen opt_index_options?
    ;

out_of_line_constraint
    : constraint_and_name? out_of_line_unique_index
    | constraint_and_name? out_of_line_primary_index[true]
    | constraint_and_name? FOREIGN KEY LeftParen column_name_list RightParen references_clause constraint_state
    | constraint_and_name? CHECK LeftParen expr RightParen constraint_state
    ;

out_of_line_primary_index[boolean using_idx_flag]
    : PRIMARY KEY LeftParen column_name_list RightParen out_of_line_index_state[$using_idx_flag]?
    ;

out_of_line_unique_index
    : UNIQUE LeftParen sort_column_list RightParen out_of_line_index_state[true]?
    ;

out_of_line_index_state[boolean using_idx_flag]
    : {!$using_idx_flag}? opt_index_options
    | {$using_idx_flag}? USING INDEX opt_index_options?
    ;

constraint_state
    : (RELY | NORELY)? ((USING INDEX opt_index_options) | (USING INDEX))? enable_option? (VALIDATE | NOVALIDATE)?
    ;

enable_option
    : ENABLE
    | DISABLE
    ;

references_clause
    : REFERENCES normal_relation_factor LeftParen column_name_list RightParen reference_option?
    | REFERENCES normal_relation_factor reference_option?
    ;

reference_option
    : ON DELETE reference_action
    ;

reference_action
    : CASCADE
    | SET NULLX
    ;

opt_generated_option_list
    : ALWAYS
    | BY DEFAULT opt_generated_identity_option
    | empty
    ;

opt_generated_identity_option
    : ON NULLX
    | empty
    ;

opt_generated_column_attribute_list
    : opt_generated_column_attribute_list generated_column_attribute
    | generated_column_attribute
    ;

generated_column_attribute
    : constraint_and_name? NOT NULLX constraint_state
    | constraint_and_name? NULLX
    | UNIQUE KEY
    | PRIMARY? KEY
    | UNIQUE
    | COMMENT STRING_VALUE
    | ID INTNUM
    | constraint_and_name? CHECK LeftParen expr RightParen constraint_state
    | SRID INTNUM
    ;

opt_identity_attribute
    : IDENTITY
    ;

column_definition_ref
    : (relation_name Dot)? column_name
    | relation_name Dot relation_name Dot column_name
    ;

column_definition_list
    : column_definition (Comma column_definition)*
    ;

column_definition_opt_datatype_list
    : column_definition_opt_datatype (Comma column_definition_opt_datatype)*
    ;

column_name_list
    : column_name (Comma column_name)*
    ;

zero_suffix_intnum
    : INTNUM
    | DECIMAL_VAL
    ;

cast_data_type
    : binary_type_i
    | character_type_i[true]
    | rowid_type_i
    | datetime_type_i
    | timestamp_type_i
    | int_type_i
    | number_type_i
    | float_type_i
    | double_type_i
    | interval_type_i
    | treat_data_type
    ;

treat_data_type
    : JSON
    | udt_type_i
    | obj_access_ref_cast
    ;

obj_access_ref_cast
    : value_or_type_name (Dot obj_access_ref_cast)?
    ;

value_or_type_name
    : NAME_OB
    ;

udt_type_i
    : (database_name Dot)? type_name
    ;

type_name
    : NAME_OB
    ;

data_type
    : int_type_i
    | float_type_i
    | double_type_i
    | number_type_i
    | timestamp_type_i
    | datetime_type_i
    | character_type_i[false] (charset_key charset_name)? collation?
    | binary_type_i
    | STRING_VALUE
    | JSON
    | XMLTYPE
    | SDO_GEOMETRY
    | interval_type_i
    | rowid_type_i
    ;

binary_type_i
    : RAW LeftParen zero_suffix_intnum RightParen
    | BLOB
    ;

float_type_i
    : FLOAT (data_type_precision | (LeftParen RightParen))?
    | REAL data_type_precision?
    ;

character_type_i [boolean in_cast_data_type]
    : CHARACTER string_length_i? BINARY?
    | CHAR string_length_i? BINARY?
    | varchar_type_i string_length_i BINARY?
    | {$in_cast_data_type}? varchar_type_i
    | NVARCHAR2 string_length_i
    | NCHAR string_length_i
    | CLOB
    ;

rowid_type_i
    : UROWID urowid_length_i?
    | ROWID urowid_length_i?
    ;

interval_type_i
    : INTERVAL YEAR year_precision=data_type_precision? TO MONTH
    | INTERVAL DAY day_precision=data_type_precision? TO SECOND second_precision=data_type_precision?
    ;

number_type_i
    : NUMBER number_precision?
    | NUMERIC number_precision?
    | DECIMAL number_precision?
    | DEC number_precision?
    ;

timestamp_type_i
    : TIMESTAMP data_type_precision?
    | TIMESTAMP data_type_precision? WITH TIME ZONE
    | TIMESTAMP data_type_precision? WITH LOCAL TIME ZONE
    ;

data_type_precision
    : LeftParen precision_int_num RightParen
    | LeftParen precision_decimal_num RightParen
    ;

int_type_i
    : SMALLINT
    | INT
    | INTEGER
    ;

varchar_type_i
    : VARCHAR
    | VARCHAR2
    ;

double_type_i
    : BINARY_DOUBLE
    | BINARY_FLOAT
    ;

datetime_type_i
    : DATE
    ;

number_precision
    : LeftParen ((Star|signed_int_num) (Comma signed_int_num)?|precision_decimal_num) RightParen
    ;

signed_int_num
    : Minus? INTNUM
    ;

precision_int_num
    : INTNUM
    ;

precision_decimal_num
    : DECIMAL_VAL
    ;

nstring_length_i
    : LeftParen zero_suffix_intnum RightParen
    ;

string_length_i
    : LeftParen zero_suffix_intnum (CHARACTER | CHAR | BYTE)? RightParen
    ;

urowid_length_i
    : LeftParen INTNUM RightParen
    ;

collation_name
    : NAME_OB
    | STRING_VALUE
    ;

trans_param_name
    : STRING_VALUE
    ;

trans_param_value
    : STRING_VALUE
    | INTNUM
    ;

charset_name
    : NAME_OB
    | STRING_VALUE
    | BINARY
    | ASCII
    ;

charset_name_or_default
    : charset_name
    | DEFAULT
    ;

collation
    : COLLATE collation_name
    ;

opt_column_attribute_list
    : opt_column_attribute_list column_attribute
    | column_attribute
    ;

column_attribute
    : constraint_and_name? NOT NULLX constraint_state
    | constraint_and_name? NULLX
    | DEFAULT bit_expr
    | ORIG_DEFAULT now_or_signed_literal
    | constraint_and_name? PRIMARY KEY
    | constraint_and_name? UNIQUE
    | ID INTNUM
    | SRID INTNUM
    | constraint_and_name? CHECK LeftParen expr RightParen constraint_state
    | constraint_and_name? references_clause constraint_state
    | SKIP_INDEX LeftParen (skip_index_type | (opt_skip_index_type_list Comma skip_index_type))? RightParen
    ;

now_or_signed_literal
    : cur_timestamp_func_params
    | signed_literal_params
    ;

cur_timestamp_func_params
    : LeftParen cur_timestamp_func_params RightParen
    | cur_timestamp_func
    ;

signed_literal_params
    : LeftParen signed_literal_params RightParen
    | signed_literal
    ;

signed_literal
    : literal
    | Plus number_literal
    | Minus number_literal
    ;

opt_comma
    : Comma?
    ;

table_option_list_space_seperated
    : table_option table_option_list_space_seperated?
    ;

table_option_list
    : table_option_list_space_seperated
    | table_option Comma table_option_list
    ;

primary_zone_name
    : DEFAULT
    | RANDOM
    | relation_name_or_string
    ;

locality_name
    : STRING_VALUE
    | DEFAULT
    ;

table_option
    : SORTKEY LeftParen column_name_list RightParen
    | parallel_option
    | TABLE_MODE COMP_EQ? STRING_VALUE
    | DUPLICATE_SCOPE COMP_EQ? STRING_VALUE
    | LOCALITY COMP_EQ? locality_name FORCE?
    | EXPIRE_INFO COMP_EQ? LeftParen bit_expr RightParen
    | PROGRESSIVE_MERGE_NUM COMP_EQ? INTNUM
    | BLOCK_SIZE COMP_EQ? INTNUM
    | TABLE_ID COMP_EQ? INTNUM
    | REPLICA_NUM COMP_EQ? INTNUM
    | compress_option
    | USE_BLOOM_FILTER COMP_EQ? BOOL_VALUE
    | PRIMARY_ZONE COMP_EQ? primary_zone_name
    | TABLEGROUP COMP_EQ? relation_name_or_string
    | read_only_or_write
    | ENGINE_ COMP_EQ? relation_name_or_string
    | TABLET_SIZE COMP_EQ? INTNUM
    | MAX_USED_PART_ID COMP_EQ? INTNUM
    | ENABLE ROW MOVEMENT
    | DISABLE ROW MOVEMENT
    | ENABLE_EXTENDED_ROWID COMP_EQ? BOOL_VALUE
    | physical_attributes_option
    | LOCATION COMP_EQ? STRING_VALUE
    | FORMAT COMP_EQ? LeftParen external_file_format_list RightParen
    | PROPERTIES COMP_EQ? LeftParen external_properties_list RightParen
    | PATTERN COMP_EQ? STRING_VALUE
    | PARTITION_TYPE COMP_EQ? USER_SPECIFIED
    | MICRO_INDEX_CLUSTERED COMP_EQ? BOOL_VALUE
    | AUTO_REFRESH COMP_EQ? (OFF|IMMEDIATE|INTERVAL)
    | ENABLE_MACRO_BLOCK_BLOOM_FILTER COMP_EQ? BOOL_VALUE
    ;

parallel_option
    : PARALLEL COMP_EQ? INTNUM
    | NOPARALLEL
    ;

storage_options_list
    : storage_option+
    ;

storage_option
    : INITIAL_ size_option
    | NEXT size_option
    | MINEXTENTS INTNUM
    | MAXEXTENTS int_or_unlimited
    ;

size_option
    : INTNUM unit_of_size?
    ;

int_or_unlimited
    : INTNUM
    | UNLIMITED
    ;

unit_of_size
    : K_SIZE
    | M_SIZE
    | G_SIZE
    | T_SIZE
    | P_SIZE
    | E_SIZE
    ;

relation_name_or_string
    : relation_name
    | STRING_VALUE
    ;

opt_equal_mark
    : COMP_EQ?
    ;

partition_option_inner
    : hash_partition_option
    | range_partition_option
    | list_partition_option
    | external_table_partition_option
    ;

external_table_partition_option
    : PARTITION BY LeftParen column_name_list RightParen
    ;

auto_partition_option
    : auto_partition_type PARTITION SIZE partition_size PARTITIONS AUTO
    ;

column_group_element
    : ALL COLUMNS
    | EACH COLUMN
    | relation_name LeftParen column_name_list RightParen
    ;

column_group_list
    : column_group_element (Comma column_group_element)*
    ;

with_column_group
    : WITH_COLUMN_GROUP LeftParen column_group_list RightParen
    ;

partition_size
    : conf_const
    | AUTO
    ;

auto_partition_type
    : auto_range_type
    ;

auto_range_type
    : PARTITION BY RANGE LeftParen column_name_list? RightParen
    ;

partition_option
    : partition_option_inner
    ;

hash_partition_option
    : PARTITION BY HASH LeftParen column_name_list RightParen subpartition_option? (PARTITIONS INTNUM)? (LeftParen hash_partition_list RightParen)? hash_partition_attributes_option_list?
    ;

hash_partition_attributes_option_list
    : TABLESPACE tablespace
    | compress_option
    | TABLESPACE tablespace compress_option
    ;

list_partition_option
    : PARTITION BY LIST LeftParen column_name_list RightParen subpartition_option? opt_list_partition_list
    ;

range_partition_option
    : PARTITION BY RANGE LeftParen column_name_list RightParen interval_option? subpartition_option? opt_range_partition_list
    ;

interval_option
    : INTERVAL LeftParen bit_expr RightParen
    ;

subpartition_option
    : subpartition_template_option
    | subpartition_individual_option
    ;

subpartition_template_option
    : SUBPARTITION BY HASH LeftParen column_name_list RightParen SUBPARTITION TEMPLATE opt_hash_subpartition_list
    | SUBPARTITION BY RANGE LeftParen column_name_list RightParen SUBPARTITION TEMPLATE opt_range_subpartition_list
    | SUBPARTITION BY LIST LeftParen column_name_list RightParen SUBPARTITION TEMPLATE opt_list_subpartition_list
    ;

subpartition_individual_option
    : SUBPARTITION BY HASH LeftParen column_name_list RightParen (SUBPARTITIONS hash_subpartition_quantity)?
    | SUBPARTITION BY RANGE LeftParen column_name_list RightParen
    | SUBPARTITION BY LIST LeftParen column_name_list RightParen
    ;

aux_column_list
    : vertical_column_name (Comma vertical_column_name)*
    ;

vertical_column_name
    : column_name
    | LeftParen column_name_list RightParen
    ;

hash_partition_list
    : hash_partition_element (Comma hash_partition_element)*
    ;

hash_partition_element
    : PARTITION relation_factor? (ID INTNUM)? hash_partition_attributes_option_list? subpartition_list?
    ;

opt_range_partition_list
    : LeftParen range_partition_list RightParen
    ;

range_partition_list
    : range_partition_element (Comma range_partition_element)*
    ;

partition_attributes_option_list
    : physical_attributes_option_list
    | compress_option
    | physical_attributes_option_list compress_option
    ;

range_partition_element
    : PARTITION relation_factor VALUES LESS THAN range_partition_expr (ID INTNUM)? partition_attributes_option_list? subpartition_list?
    | PARTITION VALUES LESS THAN range_partition_expr (ID INTNUM)? partition_attributes_option_list? subpartition_list?
    ;

opt_list_partition_list
    : LeftParen list_partition_list RightParen
    ;

list_partition_list
    : list_partition_element (Comma list_partition_element)*
    ;

list_partition_element
    : PARTITION relation_factor VALUES list_partition_expr (ID INTNUM)? partition_attributes_option_list? subpartition_list?
    | PARTITION VALUES list_partition_expr (ID INTNUM)? partition_attributes_option_list? subpartition_list?
    ;

subpartition_list
    : opt_hash_subpartition_list
    | opt_range_subpartition_list
    | opt_list_subpartition_list
    ;

opt_hash_subpartition_list
    : LeftParen hash_subpartition_list RightParen
    ;

hash_subpartition_list
    : hash_subpartition_element (Comma hash_subpartition_element)*
    ;

hash_subpartition_element
    : SUBPARTITION relation_factor physical_attributes_option_list?
    ;

opt_range_subpartition_list
    : LeftParen range_subpartition_list RightParen
    ;

range_subpartition_list
    : range_subpartition_element (Comma range_subpartition_element)*
    ;

range_subpartition_element
    : SUBPARTITION relation_factor VALUES LESS THAN range_partition_expr physical_attributes_option_list?
    ;

opt_list_subpartition_list
    : LeftParen list_subpartition_list RightParen
    ;

list_subpartition_list
    : list_subpartition_element (Comma list_subpartition_element)*
    ;

list_subpartition_element
    : SUBPARTITION relation_factor VALUES list_partition_expr physical_attributes_option_list?
    ;

list_partition_expr
    : LeftParen (DEFAULT|list_expr) RightParen
    ;

list_expr
    : bit_expr (Comma bit_expr)*
    ;

physical_attributes_option_list
    : physical_attributes_option+
    ;

physical_attributes_option
    : PCTFREE COMP_EQ? INTNUM
    | PCTUSED INTNUM
    | INITRANS INTNUM
    | MAXTRANS INTNUM
    | STORAGE LeftParen storage_options_list RightParen
    | TABLESPACE tablespace
    ;

opt_special_partition_list
    : LeftParen special_partition_list RightParen
    ;

special_partition_list
    : special_partition_define (Comma special_partition_define)*
    ;

special_partition_define
    : PARTITION (ID INTNUM)?
    | PARTITION relation_factor (ID INTNUM)?
    ;

range_partition_expr
    : LeftParen range_expr_list RightParen
    ;

range_expr_list
    : range_expr (Comma range_expr)*
    ;

range_expr
    : Plus? literal
    | Minus literal
    | access_func_expr
    | MAXVALUE
    ;

hash_subpartition_quantity
    : INTNUM
    ;

int_or_decimal
    : INTNUM
    | DECIMAL_VAL
    ;

tg_hash_partition_option
    : PARTITION BY HASH tg_subpartition_option (PARTITIONS INTNUM)?
    | PARTITION BY HASH INTNUM tg_subpartition_option (PARTITIONS INTNUM)?
    ;

tg_range_partition_option
    : PARTITION BY RANGE COLUMNS? INTNUM tg_subpartition_option opt_range_partition_list
    ;

tg_list_partition_option
    : PARTITION BY LIST COLUMNS? INTNUM tg_subpartition_option opt_list_partition_list
    ;

tg_subpartition_option
    : tg_subpartition_template_option
    | tg_subpartition_individual_option
    ;

tg_subpartition_template_option
    : SUBPARTITION BY RANGE COLUMNS? INTNUM SUBPARTITION TEMPLATE opt_range_subpartition_list
    | SUBPARTITION BY HASH SUBPARTITION TEMPLATE hash_subpartition_quantity
    | SUBPARTITION BY HASH INTNUM SUBPARTITION TEMPLATE hash_subpartition_quantity
    | SUBPARTITION BY LIST COLUMNS? INTNUM SUBPARTITION TEMPLATE opt_list_subpartition_list
    | empty
    ;

tg_subpartition_individual_option
    : SUBPARTITION BY HASH (SUBPARTITIONS hash_subpartition_quantity)?
    | SUBPARTITION BY HASH INTNUM (SUBPARTITIONS hash_subpartition_quantity)?
    | SUBPARTITION BY RANGE COLUMNS? INTNUM
    | SUBPARTITION BY LIST COLUMNS? INTNUM
    ;

opt_alter_compress_option
    : MOVE compress_option
    ;

compress_option
    : NOCOMPRESS
    | COMPRESS (BASIC | (FOR OLTP) | (FOR QUERY opt_compress_level) | (FOR ARCHIVE opt_compress_level))?
    ;

opt_compress_level
    : (LOW | HIGH)?
    ;

external_properties_list
    : external_properties (opt_comma external_properties)*
    ;

external_properties
    :  external_properties_key COMP_EQ STRING_VALUE
    ;

external_properties_key
    : ((((ACCESSID|ACCESSKEY)|(ACCESSTYPE|TYPE))|((ENDPOINT|STSTOKEN)|(PROJECT_NAME|SCHEMA_NAME)))|((COMPRESSION_CODE|QUOTA_NAME)|TABLE_NAME))
    ;

external_file_format_list
    : external_file_format (opt_comma external_file_format)*
    ;

external_file_format
    : format_key=(ENCODING|TYPE) COMP_EQ STRING_VALUE
    | format_key=(ESCAPE|FIELD_OPTIONALLY_ENCLOSED_BY|FIELD_DELIMITER|LINE_DELIMITER) COMP_EQ bit_expr
    | format_key=SKIP_HEADER COMP_EQ INTNUM
    | format_key=(SKIP_BLANK_LINES|TRIM_SPACE|EMPTY_FIELD_AS_NULL) COMP_EQ BOOL_VALUE
    | format_key=NULL_IF_EXETERNAL COMP_EQ LeftParen expr_list RightParen
    | format_key=COMPRESSION COMP_EQ compression_name
    ;

create_tablegroup_stmt
    : CREATE TABLEGROUP relation_name tablegroup_option_list? (tg_hash_partition_option | tg_range_partition_option | tg_list_partition_option)?
    ;

drop_tablegroup_stmt
    : DROP TABLEGROUP relation_name
    ;

alter_tablegroup_stmt
    : ALTER TABLEGROUP relation_name ADD TABLE? table_list
    | ALTER TABLEGROUP relation_name ((alter_partition_option|alter_tablegroup_actions)|tg_modify_partition_info)
    ;

tablegroup_option_list_space_seperated
    : tablegroup_option tablegroup_option_list_space_seperated?
    ;

tablegroup_option_list
    : tablegroup_option_list_space_seperated
    | tablegroup_option Comma tablegroup_option_list
    ;

tablegroup_option
    : LOCALITY COMP_EQ? locality_name FORCE?
    | PRIMARY_ZONE COMP_EQ? primary_zone_name
    | TABLEGROUP_ID COMP_EQ? INTNUM
    | BINDING COMP_EQ? BOOL_VALUE
    | SHARDING COMP_EQ? STRING_VALUE
    | MAX_USED_PART_ID COMP_EQ? INTNUM
    ;

alter_tablegroup_actions
    : alter_tablegroup_action (Comma alter_tablegroup_action)*
    ;

alter_tablegroup_action
    : SET? tablegroup_option_list_space_seperated
    ;

default_tablegroup
    : DEFAULT_TABLEGROUP COMP_EQ? relation_name
    | DEFAULT_TABLEGROUP COMP_EQ? NULLX
    ;

create_view_stmt
    : CREATE (OR REPLACE)? ((NO FORCE) | FORCE)? VIEW view_name (LeftParen alias_name_list RightParen)? (TABLE_ID COMP_EQ INTNUM)? AS view_subquery view_with_opt
    ;

create_mview_stmt
    : CREATE MATERIALIZED VIEW view_name (LeftParen mv_column_list RightParen)? table_option_list? (partition_option | auto_partition_option)? with_column_group? create_mview_opts? AS view_subquery view_with_opt
    ;

create_mview_opts
    : mview_refresh_opt
    | mview_refresh_opt on_query_computation_clause
    | mview_refresh_opt query_rewrite_clause
    | mview_refresh_opt on_query_computation_clause query_rewrite_clause
    | mview_refresh_opt query_rewrite_clause on_query_computation_clause
    ;

query_rewrite_clause
    : (DISABLE | ENABLE) QUERY REWRITE
    ;

on_query_computation_clause
    : (DISABLE | ENABLE) ON QUERY COMPUTATION
    ;

mview_refresh_opt
    : REFRESH mv_refresh_method mv_refresh_on_clause mv_refresh_interval
    | NEVER REFRESH
    ;

mv_refresh_on_clause
    : ON mv_refresh_mode
    | empty
    ;

mv_refresh_method
    : FAST
    | COMPLETE
    | FORCE
    ;

mv_refresh_mode
    : DEMAND
    | COMMIT
    | STATEMENT
    ;

mv_refresh_interval
    : mv_start_clause mv_next_clause
    ;

mv_start_clause
    : START WITH bit_expr
    | empty
    ;

mv_next_clause
    : NEXT bit_expr
    | empty
    ;

view_subquery
    : subquery order_by? fetch_next_clause?
    ;

view_with_opt
    : WITH READ ONLY
    | with_check_option
    | empty
    ;

with_check_option
    : WITH CHECK OPTION
    ;

view_name
    : relation_factor
    ;

opt_tablet_id
    : TABLET_ID opt_equal_mark INTNUM
    | empty
    ;

opt_tablet_id_no_empty
    : TABLET_ID opt_equal_mark INTNUM
    ;

create_index_stmt
    : CREATE UNIQUE? INDEX normal_relation_factor index_using_algorithm? ON relation_factor LeftParen sort_column_list RightParen opt_index_options? (partition_option | auto_partition_option)? with_column_group?
    | CREATE UNIQUE? INDEX normal_relation_factor index_using_algorithm? ON relation_factor LeftParen sort_column_list RightParen INDEXTYPE IS MDSYS Dot SPATIAL_INDEX opt_index_options?
    ;

index_name
    : relation_name
    ;

constraint_and_name
    : CONSTRAINT constraint_name
    ;

constraint_name
    : relation_name
    ;

sort_column_list
    : sort_column_key (Comma sort_column_key)*
    ;

sort_column_key
    : index_expr opt_asc_desc (ID INTNUM)?
    ;

index_expr
    : bit_expr
    ;

opt_index_options
    : index_option+
    ;

index_option
    : GLOBAL
    | LOCAL
    | BLOCK_SIZE COMP_EQ? INTNUM
    | COMMENT STRING_VALUE
    | STORING LeftParen column_name_list RightParen
    | WITH ROWID
    | WITH PARSER STRING_VALUE
    | index_using_algorithm
    | visibility_option
    | DATA_TABLE_ID COMP_EQ? INTNUM
    | INDEX_TABLE_ID COMP_EQ? INTNUM
    | MAX_USED_PART_ID COMP_EQ? INTNUM
    | physical_attributes_option
    | REVERSE
    | parallel_option
    ;

index_